﻿namespace PowerSong.UnitTests
    {
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using PowerSong.Indexer;
    using System.Linq;

    [TestClass]
    public class IndexingTests
        {

        [TestMethod]
        public void DocumentCountTest()
            {

            // Confirm an index can contain one file
            var index = new Indexer();
            index.AddFileContentsToIndex("File1", "Contents1");
            Assert.AreEqual(1, index.DocumentCount);

            // Confirm an index can contain a second file
            index.AddFileContentsToIndex("File2", "Contents2");
            Assert.AreEqual(2, index.DocumentCount);

            // Confirm an index can contain a third file
            index.AddFileContentsToIndex("File3", "Contents3");
            Assert.AreEqual(3, index.DocumentCount);

            }
        
        [TestMethod]
        public void SearchForOneFileAmongstOneFileTest()
            {

            // Add one document to an index
            var index = new Indexer();
            index.AddFileContentsToIndex("File1", "Contents1");

            // Ensure we can successfully find the file
            var searcher = new Searcher(index);
            var results = searcher.Search("Contents1");
            Assert.AreEqual(1, results.Count);
            Assert.AreEqual("File1", results[0]);

            }

        [TestMethod]
        public void SearchForOneFileAmongstTwoFilesTest()
            {

            // Add documents to an index
            var index = new Indexer();
            index.AddFileContentsToIndex("File1", "Contents of first file.");
            index.AddFileContentsToIndex("File2", "Contents of second file.");

            // Ensure we can successfully find the files
            var searcher = new Searcher(index);
            AssertSearch(searcher, "first", "File1");
            AssertSearch(searcher, "second", "File2");

            }

        [TestMethod]
        public void SearchForAllFilesTest()
            {

            // Add documents to an index
            var index = new Indexer();
            index.AddFileContentsToIndex("File1", "Contents of first file.");
            index.AddFileContentsToIndex("File2", "Contents of second file.");
            index.AddFileContentsToIndex("File3", "Contents of third file.");

            // Ensure we can successfully find the files
            var searcher = new Searcher(index);
            AssertSearch(searcher, "Contents", "File3", "File2", "File1");

            }

        private void AssertSearch(Searcher searcher, string query, params string[] expectedResults)
            {
            var results = searcher.Search(query);
            Assert.AreEqual(expectedResults.Length, results.Count, "Incorrect number of search results.");
            Assert.IsTrue(expectedResults.SequenceEqual(results), "The search results do not match expectations.");
            }

        }

    }
